package cn.com.hhrcw.enterprise.web;

import cn.com.hhrcw.enterprise.vo.StepVO;
import cn.com.hhrcw.entity.EnInfo;
import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.entity.SysUserDepart;
import cn.com.hhrcw.mapstruct.EnEnInfoConvert;
import cn.com.hhrcw.mapstruct.EnPiInfoConvert;
import cn.com.hhrcw.service.IEnInfoService;
import cn.com.hhrcw.service.IFileService;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.service.ISysUserDepartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 注册进度
 *
 * @program: recruit-boot-parent
 * @author: JD
 * @create: 2020-11-25 14:31
 **/
@RestController
@RequestMapping("/en/finishInfo")
@Slf4j
public class EnStepController {

    @Autowired
    IPiInfoService piInfoService;

    @Autowired
    IEnInfoService enInfoService;

    @Autowired
    ISysUserDepartService departService;

    @Autowired
    EnPiInfoConvert piInfoConvert;

    @Autowired
    EnEnInfoConvert enInfoConvert;
    @Autowired
    private ISysBaseAPI sysBaseApi;

    @Autowired
    IFileService fileService;

    /**
     * 查询当前信息填写进度
     *
     * @return
     */
    @GetMapping("/step")
    public Result<StepVO> getStep() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        StepVO stepVO = new StepVO();
        PiInfo piInfoCount = piInfoService.lambdaQuery().eq(PiInfo::getDelFlag, false).eq(PiInfo::getUserId, sysUser.getId()).one();
        List<SysUserDepart> count = departService.lambdaQuery().eq(SysUserDepart::getUserId, sysUser.getId()).list();

        if (piInfoCount == null) {
            stepVO.setStep(1);
            stepVO.setPiInfo(piInfoConvert.toVO(piInfoCount));
            return Result.ok(stepVO);
        } else {
            if (CollectionUtils.isEmpty(count)) {
                stepVO.setStep(2);
                stepVO.setPiInfo(piInfoConvert.toVO(piInfoCount));
                return Result.ok(stepVO);
            } else {
                String id = count.get(0).getDepId();
                EnInfo departInfo = enInfoService.lambdaQuery().eq(EnInfo::getId, id).eq(EnInfo::getDelFlag, false).one();
                if (departInfo == null) {
                    stepVO.setStep(2);
                    stepVO.setPiInfo(piInfoConvert.toVO(piInfoCount));
                    stepVO.setEnInfo(enInfoConvert.toVO(departInfo));
                    return Result.ok(stepVO);
                } else {
                    stepVO.setPiInfo(piInfoConvert.toVO(piInfoCount));
                    stepVO.setEnInfo(enInfoConvert.toVO(departInfo));
                    stepVO.setStep(3);
                    return Result.ok(stepVO);
                }
            }
        }
    }
}
